package com.thinkin.storage.jincang.buz;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.allDo.SysDao;
import com.thinkin.storage.jincang.dao.WorderinDao;
import com.thinkin.storage.jincang.dao.impl.WorderinDaoImpl;
import com.thinkin.storage.jincang.po.Worderin;
import com.thinkin.storage.jincang.po.WorderinSku;
import com.thinkin.storage.jincang.vo.WorderinVo;
import com.thinkin.util.JsonHelper;

public class WorderinBuz {
	Logger logger = LoggerFactory.getLogger(WorderinBuz.class);
	private WorderinDao worderinDao = null;

	public WorderinBuz() {
		worderinDao = new WorderinDaoImpl();
	}

	public String doSaveAdd(Worderin pvo, List<WorderinSku> skuitems) {

		return null;

	}

	public String doQuery(WorderinVo qvo, String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer
				.append("SELECT keyid,dictatetype,orderno,orderdate,iorgname,orgname,uploaddate,skuname,quantity,piece,grossweight,netweight,volume,checkname,checkdate,orderstatus FROM "
						+ clazz + " WHERE 1=1");
		if (StringUtils.isNotBlank(qvo.getOrderno())) {
			stringBuffer.append(" AND orderno=" + qvo.getOrderno());
		}
		if (StringUtils.isNotBlank(qvo.getIorgcd())) {
			stringBuffer.append(" AND iorgcd=" + qvo.getIorgcd());
		}
		if (StringUtils.isNotBlank(qvo.getOrderdatefrom())) {
			stringBuffer.append(" AND orderdate=to_date('"
					+ qvo.getOrderdatefrom() + "','yyyy-mm-dd')");
		}
		if (StringUtils.isNotBlank(qvo.getOrderdateto())) {
			stringBuffer.append(" AND orderdate=to_date('"
					+ qvo.getOrderdateto() + "','yyyy-mm-dd')");
		}
		if (StringUtils.isNotBlank(qvo.getOrderstatus())) {
			stringBuffer.append(" AND orderstatus=" + qvo.getOrderstatus());
		}
		if (StringUtils.isNotBlank(qvo.getOrgcd())) {
			stringBuffer.append(" AND orgcd=" + qvo.getOrgcd());
		}
		if (StringUtils.isNotBlank(qvo.getSkuname())) {
			stringBuffer.append(" AND skuname like '%" + qvo.getSkuname()
					+ "%'");
		}
		if (StringUtils.isNotBlank(qvo.getUploaddatefrom())) {
			stringBuffer.append(" AND uploaddate=to_date('"
					+ qvo.getUploaddatefrom() + "','yyyy-mm-dd')");
		}
		if (StringUtils.isNotBlank(qvo.getUploaddateto())) {
			stringBuffer.append(" AND uploaddate=to_date('"
					+ qvo.getUploaddateto() + "','yyyy-mm-dd')");
		}
		List<Map<String, Object>> maps = worderinDao.doQuery(
				stringBuffer.toString(), true);

		result = JsonHelper.toJSONString(maps);
		return result;
	}

	/**
	 * 初始化组织
	 * 
	 * @param clazz
	 * @return
	 */
	public String initZorg(String clazz) {

		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT * FROM " + clazz + " WHERE 1=1 ");

		List<Map<String, Object>> maps = worderinDao.doQuery(
				stringBuffer.toString(), true);
		result = JsonHelper.toJSONString(maps);
		return result;
	}

	/**
	 * 货主、寄仓单位、供应商
	 */
	public String initCus(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT keyid as value, cusnamecn as label FROM "
				+ clazz + " WHERE 1=1 ");
		List<Map<String, Object>> maps = worderinDao.doQuery(
				stringBuffer.toString(), true);
		result = JsonHelper.toJSONString(maps);
		return result;
	}

	/**
	 * 初始化 重量、体积、毛重、净重、单位
	 * 
	 * @param param
	 * @param clazz
	 * @return
	 */
	public static String initEunit(String param, String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT keyid as value,unitname as label FROM "
				+ clazz + " WHERE 1=1 ");
		stringBuffer.append(" AND typelist like '%" + param + "%' ");
		stringBuffer.append(" AND stateid like 'T' ");
		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);

		result = JsonHelper.toJSONString(maps);
		return result;
	}

	public static String initCurrcd(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT keyid as value,namecn as label FROM "
				+ clazz + " WHERE 1=1 ");

		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);

		result = JsonHelper.toJSONString(maps);
		return result;
	}

	public static String initQuanlity(String clazz) {
		String result = "{}";
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("SELECT keyid as value,quanlityname as label FROM "
				+ clazz + " WHERE 1=1 ");

		List<Map<String, Object>> maps = SysDao.getCommonServer().doQuery(
				stringBuffer.toString(), true);

		result = JsonHelper.toJSONString(maps);
		return result;
	}

}
